package ann;

public class Node {
	
	private Layer parentLayer;
	private double membranePotential;
	private double activationLevel;
	private double previousActivationLevel;
	private double outError;
	private double inError;
	
	public Node(Layer parentLayer)
	{
		this.parentLayer = parentLayer;
		membranePotential = 0;
	}

	public void setMembranePotential(double j) {
		membranePotential = j;
	}

	public double getActivationLevel() {
		return activationLevel;
	}

	public double getMembranePotential() {
		return membranePotential;
	}

	public void updateActivationLevel() {
		previousActivationLevel = activationLevel;
		activationLevel = parentLayer.getActivationFunction().getActivation(membranePotential);
	}

	public void setTarget(double target) {
		outError = target-activationLevel;
	}

	public void propageteError() {
		inError = outError*parentLayer.getActivationFunction().getActivationDerivative(membranePotential);
	}

	public void clearInError() {
		inError = 0.0;
	}

	public void clearOutError() {
		outError=0.0;
	}

	public double getInError() {
		
		return inError;
	}

	public void incrementOutError(double increment) {
		outError += increment;
	}
	
	public String toString()
	{
		String ret = "Node {";
		
		ret+=tr(membranePotential)+"("+tr(inError)+")";
		ret+=" -> ";
		ret+=tr(activationLevel)+"("+tr(outError)+")";
		
		return ret+"}";
	}
	
	private double tr(double value)
	{
		return ((int)(value*1000))/1000.0;
	}
}
